﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoClubGorbea.Model;
using VideoClubGorbea.Infraestructure.DomainBase;
using VideoClubGorbea.Infraestructure.Repositories;

namespace VideoClubGorbea.Infraestructure.RepositoryFramework
{
    public static class RepositoryFatory
    {
        private static Dictionary<string, object> repositories = new Dictionary<string, object>();

        public static TRepository GetRepository<TRepository, TEntity>()
            where TRepository : class, IRepository<TEntity>
            where TEntity : EntityBase
        {

            TRepository repository = default(TRepository);
            string interfaceShortName = typeof(TRepository).Name;

            if (!RepositoryFatory.repositories.ContainsKey(interfaceShortName))
            {
                repository = new GorbeaRepository(new UnitOfWork()) as TRepository;
                RepositoryFatory.repositories.Add(interfaceShortName, repository);
            }
            else
            {
                repository = (TRepository)RepositoryFatory.repositories[interfaceShortName];
            }

            return repository;

        }
    }
}
